import { defineStore } from "pinia"
import { ref, computed } from "vue"
import { pinia } from "@/pinia"
import { useLeagueStore } from "./sub/leagueStore"
import { useSignalStore } from "./sub/signalStore"
import { useChartSubStore } from "./sub/chartStore"
import { useSimilarMatchStore } from "./sub/similarMatchStore"

export const useChartStore = defineStore("chart", () => {
  // 获取子 stores
  const leagueStore = useLeagueStore()
  const signalStore = useSignalStore()
  const chartSubStore = useChartSubStore()
  const similarMatchStore = useSimilarMatchStore()

  // #region 组合 Actions - 保持原有 API 兼容性
  
  /** 当用户选择比赛时，获取两个公司的信号数据 */
  async function selectLeague(leagueId: string) {
    const league = leagueStore.selectLeague(leagueId)
    if (!league) {
      return
    }

    // 重置信号数据
    signalStore.resetSignalData()
    // 确保子 store 已初始化后再调用
    if (chartSubStore.resetAllCharts) {
      chartSubStore.resetAllCharts()
    }

    // 获取信号数据
    const result = await signalStore.fetchSignals(league.fid)
    
    // 生成图表数据
    if (result.company293Success || result.company18Success) {
      // 生成单个公司图表
      if (result.company293Success) {
        chartSubStore.generateCompanyChart(signalStore.company293Signals, true)
      }
      if (result.company18Success) {
        chartSubStore.generateCompanyChart(signalStore.company18Signals, false)
      }
      
      // 生成组合图表
      chartSubStore.generateCombinedChartData(
        signalStore.company293Signals,
        signalStore.company18Signals
      )
    }
  }

  /** 重置所有数据 */
  function resetAllData() {
    leagueStore.resetAllData()
    signalStore.resetSignalData()
    chartSubStore.resetAllCharts()
    similarMatchStore.resetAllSimilarMatchData()
  }

  /** 重置信号相关数据 */
  function resetSignalData() {
    signalStore.resetSignalData()
    chartSubStore.resetAllCharts()
  }

  // #endregion

  // 使用 computed 来确保响应式，并提供 getter/setter
  const leagueOptions = computed(() => leagueStore.leagueOptions)
  const selectedLeague = computed({
    get: () => leagueStore.selectedLeague,
    set: (value) => { leagueStore.selectedLeague = value }
  })
  const isLoadingLeagues = computed(() => leagueStore.isLoadingLeagues)
  const parsedLeagueInfo = computed(() => leagueStore.parsedLeagueInfo)
  const eventOptions = computed(() => leagueStore.eventOptions)
  const selectedEvent = computed({
    get: () => leagueStore.selectedEvent,
    set: (value) => { leagueStore.selectedEvent = value }
  })
  const isLoadingEvents = computed(() => leagueStore.isLoadingEvents)
  
  const isLoadingSignals = computed(() => signalStore.isLoadingSignals)
  const company293Signals = computed(() => signalStore.company293Signals)
  const company18Signals = computed(() => signalStore.company18Signals)
  const leftTableData = computed(() => signalStore.leftTableData)
  const rightTableData = computed(() => signalStore.rightTableData)
  
  const leftBottomChart293 = computed(() => chartSubStore.leftBottomChart293)
  const leftBottomChart18 = computed(() => chartSubStore.leftBottomChart18)
  const rightCombinedChartHome = computed(() => chartSubStore.rightCombinedChartHome)
  const rightCombinedChartDraw = computed(() => chartSubStore.rightCombinedChartDraw)
  const rightCombinedChartAway = computed(() => chartSubStore.rightCombinedChartAway)
  
  const isLoadingSimilarMatches = computed(() => similarMatchStore.isLoadingSimilarMatches)
  const isLoadingSimilarMatchSignals = computed(() => similarMatchStore.isLoadingSimilarMatchSignals)
  const similarMatches = computed(() => similarMatchStore.similarMatches)
  const similarMatchesPagination = computed(() => similarMatchStore.similarMatchesPagination)
  const similarMatchesStatistics = computed(() => similarMatchStore.similarMatchesStatistics)
  const selectedSimilarMatch = computed(() => similarMatchStore.selectedSimilarMatch)
  const similarMatch293Signals = computed(() => similarMatchStore.similarMatch293Signals)
  const similarMatch18Signals = computed(() => similarMatchStore.similarMatch18Signals)
  const similarMatchLeftTableData = computed(() => similarMatchStore.similarMatchLeftTableData)
  const similarMatchRightTableData = computed(() => similarMatchStore.similarMatchRightTableData)
  const similarMatchLeftBottomChart293 = computed(() => similarMatchStore.similarMatchLeftBottomChart293)
  const similarMatchLeftBottomChart18 = computed(() => similarMatchStore.similarMatchLeftBottomChart18)
  const similarMatchRightCombinedChartHome = computed(() => similarMatchStore.similarMatchRightCombinedChartHome)
  const similarMatchRightCombinedChartDraw = computed(() => similarMatchStore.similarMatchRightCombinedChartDraw)
  const similarMatchRightCombinedChartAway = computed(() => similarMatchStore.similarMatchRightCombinedChartAway)
  const similarMatchDataMap = computed(() => similarMatchStore.similarMatchDataMap)

  return {
    // League Store 的状态和方法
    leagueOptions,
    selectedLeague,
    isLoadingLeagues,
    parsedLeagueInfo,
    eventOptions,
    selectedEvent,
    isLoadingEvents,
    fetchLeaguesByDateRange: leagueStore.fetchLeaguesByDateRange,
    fetchEventList: leagueStore.fetchEventList,
    
    // Signal Store 的状态和方法
    isLoadingSignals,
    company293Signals,
    company18Signals,
    leftTableData,
    rightTableData,
    fetchSignals: signalStore.fetchSignals,
    processCompany293Data: signalStore.processCompany293Data,
    processCompany18Data: signalStore.processCompany18Data,
    
    // Chart Store 的状态和方法
    leftBottomChart293,
    leftBottomChart18,
    rightCombinedChartHome,
    rightCombinedChartDraw,
    rightCombinedChartAway,
    generateCompanyChart: chartSubStore.generateCompanyChart,
    generateCombinedChartData: chartSubStore.generateCombinedChartData,
    resetCombinedCharts: chartSubStore.resetCombinedCharts,
    resetAllCharts: chartSubStore.resetAllCharts,
    
    // Similar Match Store 的状态和方法
    isLoadingSimilarMatches,
    isLoadingSimilarMatchSignals,
    similarMatches,
    similarMatchesPagination,
    similarMatchesStatistics,
    selectedSimilarMatch,
    similarMatch293Signals,
    similarMatch18Signals,
    similarMatchLeftTableData,
    similarMatchRightTableData,
    similarMatchLeftBottomChart293,
    similarMatchLeftBottomChart18,
    similarMatchRightCombinedChartHome,
    similarMatchRightCombinedChartDraw,
    similarMatchRightCombinedChartAway,
    similarMatchDataMap,
    fetchSimilarMatches: similarMatchStore.fetchSimilarMatches,
    selectSimilarMatch: similarMatchStore.selectSimilarMatch,
    processSimilarMatchCompanyData: similarMatchStore.processSimilarMatchCompanyData,
    generateSimilarMatchCombinedChartData: similarMatchStore.generateSimilarMatchCombinedChartData,
    resetSimilarMatchCombinedCharts: similarMatchStore.resetSimilarMatchCombinedCharts,
    resetSimilarMatchSignalData: similarMatchStore.resetSimilarMatchSignalData,
    resetAllSimilarMatchData: similarMatchStore.resetAllSimilarMatchData,
    
    // 组合方法
    selectLeague,
    resetAllData,
    resetSignalData
  }
})

export function useChartStoreOutside() {
  return useChartStore(pinia)
}
